﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BI.Datasets;
using BI.Properties;

namespace BI
{
    public partial class TableNameDialog : Form
    {
        int locMode;
        string locTableName;
        MainForm mainForm;

        /// <summary>
        /// Возвращает текущую систему
        /// </summary>
        private IBISystem curBISystem
        {
            get
            {
                return mainForm.BISystemLink;
            }
        }

        public TableNameDialog(MainForm pf, int parMode, string parTableName)
        {
            mainForm = pf;
            locMode = parMode;
            locTableName = parTableName;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // проверка данных			
            // имя
            tboxTableName.Text = tboxTableName.Text.Trim();
            if (tboxTableName.Text == "")
            {
                MessageBox.Show(Resources.RS_DB_ERROR_IS_EMPTY);
                tboxTableName.Focus();
                return;
            }
            int c = MetadataAndData.data.UserTablesCount();
            string newname = tboxTableName.Text;
            for (int i = 0; i < c; i++)
                if (MetadataAndData.data.GetUserTableNameByIndex(i) == newname)
                {
                    MessageBox.Show(Resources.RS_DB_ERROR_ALREADY_EXISTS);
                    tboxTableName.Focus();
                    return;
                }
            if (MetadataAndData.data.IsSystemTable(newname))
            {
                MessageBox.Show(Resources.RS_DB_ERROR_TABLE_NAME_IS_SYSTEM);
                tboxTableName.Focus();
                return;
            }
            if (MetadataAndData.data.UpdateUserTable(locTableName, newname))
            {            
                Hashtable tableCharacteristics = curBISystem.GetAllTableCharacteristicsByTableName(locTableName);
                //переименовываем ссылки на таблицу во всех характеристиках
                IEnumerator en = tableCharacteristics.GetEnumerator();
                while (en.MoveNext())
                {
                    Characteristic ch = ((DictionaryEntry)en.Current).Value as Characteristic;
                    ch.expectedValue = newname;
                }
            }
            //НЕ ПОНИМАЮ ЗАЧЕМ СРАЗУ СОХРАНЯТЬmainForm.SaveDataBase();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void NewTableForm_Load(object sender, EventArgs e)
        {
            if (locMode == 1)
            {
                tboxTableName.Text = locTableName;                
            }
        }
    }
}
